package com.qiangshi.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiangshi.createTableLog.domain.QsCreateTableLog;
import com.qiangshi.createTableLog.service.IQsCreateTableLogService;
import com.qiangshi.realTimeInformation.service.IQsRealTimeInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 实时信息定时任务
 *
 * @author ccm
 */
@Component("realTimeInformationTask")
public class RealTimeInformationTask {
    @Autowired
    IQsRealTimeInformationService qsRealTimeInformationService;
    @Autowired
    IQsCreateTableLogService qsCreateTableLogService;

    /**
     * 实时信息分表处理
     */
    public void handleRealTimeInformation() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        List<String> tableNameList = new ArrayList<>();
        List<QsCreateTableLog> createTableLogList = qsCreateTableLogService.list(new QueryWrapper<QsCreateTableLog>().lambda().eq(QsCreateTableLog::getTableType, 1));
        List<QsCreateTableLog> createTableLogNewList = new ArrayList<>();
        for (QsCreateTableLog qsCreateTableLog : createTableLogList) {
            tableNameList.add(qsCreateTableLog.getTableName() + qsCreateTableLog.getNameSuffix() + "_" + format.format(calendar.getTime()));
            QsCreateTableLog newCreateTableLog = QsCreateTableLog.builder().tableName(qsCreateTableLog.getTableName()).nameSuffix(qsCreateTableLog.getNameSuffix() + "_" + format.format(calendar.getTime()))
                    .tableType(2).caseNumber(qsCreateTableLog.getCaseNumber()).monthNumber(format.format(calendar.getTime())).build();
            createTableLogNewList.add(newCreateTableLog);
        }
        qsRealTimeInformationService.createNewTable(tableNameList);
        qsRealTimeInformationService.handleRealTimeInformation(tableNameList);
        qsCreateTableLogService.saveBatch(createTableLogNewList);
    }


}
